class TextDeduplicator {
    static deduplicate(text, options = {}) {
        try {
            const {
                sortOrder = 'asc',
                caseSensitive = true,
                removeEmptyLines = true,
                trimWhitespace = true,
                delimiter = '\n'
            } = options;

            if (!text || typeof text !== 'string') {
                return { success: false, error: '请输入有效的文本内容' };
            }

            // 按分隔符分割文本
            let lines = text.split(delimiter);
            
            // 统计原始行数
            const originalCount = lines.length;

            // 处理空行
            if (removeEmptyLines) {
                lines = lines.filter(line => line.trim() !== '');
            }

            // 处理空白字符
            if (trimWhitespace) {
                lines = lines.map(line => line.trim());
            }

            // 去重处理
            let uniqueLines;
            if (caseSensitive) {
                uniqueLines = [...new Set(lines)];
            } else {
                const lowerCaseMap = new Map();
                lines.forEach(line => {
                    const lowerLine = line.toLowerCase();
                    if (!lowerCaseMap.has(lowerLine)) {
                        lowerCaseMap.set(lowerLine, line);
                    }
                });
                uniqueLines = Array.from(lowerCaseMap.values());
            }

            // 排序处理
            if (sortOrder === 'asc') {
                uniqueLines.sort((a, b) => {
                    if (!caseSensitive) {
                        return a.toLowerCase().localeCompare(b.toLowerCase());
                    }
                    return a.localeCompare(b);
                });
            } else if (sortOrder === 'desc') {
                uniqueLines.sort((a, b) => {
                    if (!caseSensitive) {
                        return b.toLowerCase().localeCompare(a.toLowerCase());
                    }
                    return b.localeCompare(a);
                });
            }
            // sortOrder === 'none' 时不排序

            const result = uniqueLines.join(delimiter);
            const stats = {
                originalCount,
                uniqueCount: uniqueLines.length,
                removedCount: originalCount - uniqueLines.length,
                duplicateRate: originalCount > 0 ? ((originalCount - uniqueLines.length) / originalCount * 100).toFixed(2) : 0
            };

            return { 
                success: true, 
                data: {
                    result,
                    stats
                }
            };

        } catch (error) {
            return { success: false, error: `处理文本时出错: ${error.message}` };
        }
    }

    static parseCSVLine(line) {
        try {
            const result = [];
            let current = '';
            let inQuotes = false;
            
            for (let i = 0; i < line.length; i++) {
                const char = line[i];
                const nextChar = line[i + 1];
                
                if (char === '"') {
                    if (inQuotes && nextChar === '"') {
                        current += '"';
                        i++; // 跳过下一个引号
                    } else {
                        inQuotes = !inQuotes;
                    }
                } else if (char === ',' && !inQuotes) {
                    result.push(current);
                    current = '';
                } else {
                    current += char;
                }
            }
            
            result.push(current);
            return { success: true, data: result };
            
        } catch (error) {
            return { success: false, error: `解析CSV行时出错: ${error.message}` };
        }
    }

    static deduplicateByColumn(text, options = {}) {
        try {
            const {
                columnIndex = 0,
                delimiter = ',',
                hasHeader = true,
                sortOrder = 'asc',
                caseSensitive = true
            } = options;

            if (!text || typeof text !== 'string') {
                return { success: false, error: '请输入有效的CSV文本内容' };
            }

            const lines = text.split('\n').filter(line => line.trim() !== '');
            if (lines.length === 0) {
                return { success: false, error: '文本为空' };
            }

            let dataLines = lines;
            let header = null;

            if (hasHeader) {
                header = lines[0];
                dataLines = lines.slice(1);
            }

            const seenValues = new Set();
            const uniqueLines = [];

            for (const line of dataLines) {
                const parseResult = this.parseCSVLine(line);
                if (!parseResult.success) {
                    continue; // 跳过解析失败的行
                }

                const columns = parseResult.data;
                if (columns.length <= columnIndex) {
                    continue; // 跳过列数不足的行
                }

                const keyValue = caseSensitive 
                    ? columns[columnIndex].trim()
                    : columns[columnIndex].trim().toLowerCase();

                if (!seenValues.has(keyValue)) {
                    seenValues.add(keyValue);
                    uniqueLines.push({
                        originalLine: line,
                        sortKey: columns[columnIndex].trim()
                    });
                }
            }

            // 排序
            if (sortOrder !== 'none') {
                uniqueLines.sort((a, b) => {
                    const aKey = caseSensitive ? a.sortKey : a.sortKey.toLowerCase();
                    const bKey = caseSensitive ? b.sortKey : b.sortKey.toLowerCase();
                    
                    if (sortOrder === 'asc') {
                        return aKey.localeCompare(bKey);
                    } else {
                        return bKey.localeCompare(aKey);
                    }
                });
            }

            const resultLines = uniqueLines.map(item => item.originalLine);
            if (header) {
                resultLines.unshift(header);
            }

            const result = resultLines.join('\n');
            const originalCount = dataLines.length;
            const uniqueCount = uniqueLines.length;

            const stats = {
                originalCount,
                uniqueCount,
                removedCount: originalCount - uniqueCount,
                duplicateRate: originalCount > 0 ? ((originalCount - uniqueCount) / originalCount * 100).toFixed(2) : 0
            };

            return { 
                success: true, 
                data: {
                    result,
                    stats
                }
            };

        } catch (error) {
            return { success: false, error: `按列去重时出错: ${error.message}` };
        }
    }
}